package com.lanou.renrenxin.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.base.BaseActivity;
import com.lanou.renrenxin.custommodule.ZoomImageView;
import com.lanou.renrenxin.singleton.RenrenxinSingleton;
import com.lanou.renrenxin.tools.StringValues;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by zouhui on 16/3/17.
 */
public class ImageActivity extends BaseActivity implements View.OnClickListener {
    private ZoomImageView imageView;
    private ImageButton backBtn;
    private Button saveImage;
    private String path;
    private boolean isSend;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        Intent intent = getIntent();
        path = intent.getStringExtra(StringValues.IMAGE_PATH);
        isSend = intent.getBooleanExtra(StringValues.IMAGE_ISSEND, false);

        imageView = (ZoomImageView) findViewById(R.id.iv_activity_image);
        backBtn = (ImageButton) findViewById(R.id.imgBtn_activity_image);
        saveImage = (Button) findViewById(R.id.btn_activity_image_saveImage);
        backBtn.setOnClickListener(this);
        saveImage.setOnClickListener(this);
        if (isSend) {
            bitmap = getLoacalBitmap(path);
            imageView.setImageBitmap(bitmap);
            imageView.setVisibility(View.VISIBLE);
        } else {
            ImageRequest imageRequest = new ImageRequest(path, new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    bitmap = response;
                    imageView.setImageBitmap(response);
                }

            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    imageView.setImageResource(R.mipmap.ic_renrenxin_load_field);
                }
            });
            RenrenxinSingleton.getInstence().addImageRequest(imageRequest);
        }


    }

    private Bitmap getLoacalBitmap(String path) {
        try {
            FileInputStream fis = new FileInputStream(path);
            return BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgBtn_activity_image:
                finish();
                break;
            case R.id.btn_activity_image_saveImage:
                saveImage();
                break;
        }
    }

    private void saveImage() {
        String cacheDir = null;
        if (bitmap != null) {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//SD卡是否挂载
                File root = Environment.getExternalStorageDirectory();
                File ruiCache = new File(root.getAbsolutePath() + "/Renrenxin");
                if (!ruiCache.exists()) {
                    ruiCache.mkdir();
                }
                cacheDir = ruiCache.getAbsolutePath();
            }else {
                Toast.makeText(this, "没有SD卡", Toast.LENGTH_SHORT).show();
            }
        }
        if (path.contains(".jpg")||path.contains(".png")) {
            path = path.substring(path.lastIndexOf("/"), path.lastIndexOf(".") + 4);
            FileOutputStream fos = null;
            try {
                //初始化文件写入对象   添加路径
                fos = new FileOutputStream(cacheDir + "/" + path);
                //保存bitmap
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                if(null != fos){
                    try {
                        //这么写 怎么都会关
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                Toast.makeText(this, "已经保存到"+cacheDir, Toast.LENGTH_SHORT).show();
            }
        }
    }
}
